# -*- coding: utf-8 -*-

"""rmis URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.conf.urls import url, include
import xadmin
from django.views.static import serve
from django.views.generic.base import TemplateView, RedirectView
from django.apps import apps
from django.contrib.auth import get_user_model
from django.shortcuts import redirect
from rmis.settings import MEDIA_ROOT
from rest_framework.documentation import include_docs_urls
from rest_framework.routers import DefaultRouter
from good.views import *
from user.views import *
from xadmin.plugins.excel import ImportExcelView
from django.conf import settings
from django.conf.urls.static import static

# router = DefaultRouter()
# router.register(r'goods', GoodListViewSet)
# router.register(r'goodmultimedias', GoodMultimediaListViewSet)
# router.register(r'with-detailed-good-goodmultimedias', GoodMultimediaWithDetailedGoodListViewSet)
# 配置同一model的不同Serializer会出问题 不同URL生效的Serializer相同

def sub_user_change_password(request, sub_user, sub_user_id, *args, **kwargs):
    app = apps.get_app_config("user")
    sub_user_model = app.get_model(sub_user)
    some_sub_user = sub_user_model.objects.get(id=sub_user_id)
    # print(model_to_dict(user))
    user_model = get_user_model()
    some_user = user_model.objects.get(username=some_sub_user.username)
    return redirect('/xadmin/user/userprofile/{0}/password'.format(some_user.id))


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^xadmin/user/(?P<sub_user>(clerk)|(customer)|(administrator))/(?P<sub_user_id>\d+)/password/$',sub_user_change_password),
    url(r'^xadmin/', xadmin.site.urls),
    url(r'^ueditor/', include('DjangoUeditor.urls')),  # 富文本相关URL
    url(r'^media/(?P<path>.*)', serve, {"document_root": MEDIA_ROOT}),  # 能加载meida路径下的文件
    url(r'^api-auth/', include('rest_framework.urls')),
    url(r'^docs/', include_docs_urls(title='rmis-url-doc')),
    url(r'^api-invoice/', include('invoice.urls')),
    url(r'^api-label/', include('label.urls')),
    url(r'^api-organization/', include('organization.urls', namespace="organization", app_name="organization")),
    # namespace app_name
    # Must specify a namespace if specifying app_name.
    url(r'^api-user/', include('user.urls')),
    url(r'^api-good/', include('good.urls')),
    url(r'^api-analysis/', include('page.urls')),
    # url(r'^rmis/api/', include(router.urls)),
    url(r'^upload/app/(?P<app>\w+)/model/(?P<model>\w+)', ImportExcelView.as_view()),
    url(r'^create/app/(?P<app>\w+)/model/(?P<model>\w+)', ImportExcelView.as_view()),
    url(r'^favicon.ico$', RedirectView.as_view(url='/media/favicon.ico')),
]

# http://127.0.0.1:8000/xadmin/user/customer/420/password/
